<template>
  <el-dialog :title="!dataForm.deviceId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="设备类型" prop="deviceType" v-if="!dataForm.deviceId">
        <el-select v-model="dataForm.deviceList.name" @change="getVendorList" clearable placeholder="选择设备类型">
          <el-option v-for="item in deviceList" :value="item.name" :label="item.name" :key="item.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备厂商" prop="vendorId" v-if="!dataForm.deviceId">
        <el-select v-model="dataForm.deviceList.from" style="width: 120px;" @change="getTypeList" placeholder="选择设备厂商">
          <el-option v-for="item in vendorList" :value="item.from" :label="item.from" :key="item.from"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备型号" prop="typeId" v-if="!dataForm.deviceId">
        <el-select v-model="dataForm.deviceList.type" style="width: 120px;" @change="getDeviceId" placeholder="选择设备型号">
          <el-option v-for="item in typeList" :value="item.type" :label="item.type" :key="item.type"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="指令名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="指令名称"></el-input>
      </el-form-item>
      <el-form-item label="指令" prop="command">
        <el-input v-model="dataForm.command" placeholder="指令"></el-input>
      </el-form-item>
      <el-form-item label="实际值" prop="value">
        <el-input v-model="dataForm.value" placeholder="实际值"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    getPvmDeviceCommandObj,
    addPvmDeviceCommandObj,
    putPvmDeviceCommandObj
  } from '@/api/pvm/pvmdevicecommand'
  import {
    getDevice,
    getList,
    getPvmDeviceObj
  } from "@/api/pvm/pvmdevice";

  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          deviceId: 0,
          groupId: 0,
          name: '',
          command: '',
          value: '',
          createTime: '',
          updateTime: '',
          createUser: '',
          updateUser: '',
          unitId: '',
          tenantId: '',
          delFlag: '',
          deviceList: {}
        },
        deviceList: {},
        vendorList: {},
        typeList: {},
        dataRule: {
          name: [{
            required: true,
            message: '指令名称不能为空',
            trigger: 'blur'
          }],
          command: [{
            required: true,
            message: '指令不能为空',
            trigger: 'blur'
          }],
          value: [{
            required: true,
            message: '实际值不能为空',
            trigger: 'blur'
          }],
          createTime: [{
            required: true,
            message: '创建时间不能为空',
            trigger: 'blur'
          }],
          updateTime: [{
            required: true,
            message: '修改时间不能为空',
            trigger: 'blur'
          }],
          createUser: [{
            required: true,
            message: '创建用户id不能为空',
            trigger: 'blur'
          }],
          updateUser: [{
            required: true,
            message: '修改用户id不能为空',
            trigger: 'blur'
          }],
          unitId: [{
            required: true,
            message: '单位id不能为空',
            trigger: 'blur'
          }],
          tenantId: [{
            required: true,
            message: '租户id不能为空',
            trigger: 'blur'
          }],
          delFlag: [{
            required: true,
            message: '是否删除(-1删除)不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      init(id, groupId) {
        this.dataForm.deviceId = id || 0
        this.dataForm.groupId = groupId || 0
        this.visible = true
        getList("device", this.dataForm).then(response => {
          if(response.data.code == '0'){
            this.deviceList = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.deviceId) {
            getPvmDeviceCommandObj(this.dataForm.deviceId, this.dataForm.groupId).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
                this.dataForm.deviceList = {}
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.deviceList) {
              this.dataForm.deviceName = this.dataForm.deviceList.name
              this.dataForm.deviceFrom = this.dataForm.deviceList.from
              this.dataForm.deviceType = this.dataForm.deviceList.type
            }
            if (this.dataForm.deviceId) {
              putPvmDeviceCommandObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            } else {
              addPvmDeviceCommandObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            }
          }
        })
      },
      getVendorList() {
        getList("vendor", this.dataForm.deviceList).then(response => {
          if(response.data.code == '0'){
            this.vendorList = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      getTypeList() {
        getList("type", this.dataForm.deviceList).then(response => {
          if(response.data.code == '0'){
            this.typeList = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      getDeviceId() {
        getDevice(this.dataForm.deviceList).then(response => {
          if(response.data.code == '0'){
            this.dataForm.deviceList = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
    }
  }
</script>
